﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Northwind.V2.Domain;

namespace Ex01
{
    class Program
    {
        static void Main(string[] args)
        {
            //设置数据库初始化策略
            Database.SetInitializer(new NorthwindInitializer());
            //0.Retrieve--select 
            using (var context = new NorthwindDbContext())
            {
                var customer = context.Customers.Find(1);
                Console.WriteLine(customer);
            }
            //1.Create --insert a new Customer(1)
            using (var context = new NorthwindDbContext())
            {
                //方法1
                //var newCustomer = new Customer { ID=100,Name="draco"};
                // context.Customers.Add(newCustomer);
                
                //方法2
                var newCustomer = new Customer { ID = 100, Name = "draco" };
                context.Customers.Attach(newCustomer);
                context.Entry(newCustomer).State=EntityState.Added;
                
                context.SaveChanges();
            }
            //2.Modify ---update 
            using (var context = new NorthwindDbContext())
            {
                //方法1
                //var customer = context.Customers.Find(1);
                //customer.Name = "Modified";
                
                //方法2
                var customer = new Customer { ID=1,Name="Modified"};
                context.Customers.Attach(customer);
                context.Entry(customer).State=EntityState.Modified;
                
                context.SaveChanges();
            }
            //3.Delete delete
            using (var context = new NorthwindDbContext())
            {
                //方法1
                //var customer = context.Customers.Find(1);
                //context.Customers.Remove(customer);
               
                //方法2
                var customer = new Customer { ID=1};
                context.Customers.Attach(customer);
                context.Entry(customer).State=EntityState.Deleted;
                
                context.SaveChanges();
            }
            Console.ReadKey();
        }
    }
}
